//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.dalmatian.common.redis.interceptor;

import org.dalmatian.common.redis.annotation.CacheEvict;
import org.dalmatian.common.redis.annotation.CachePut;
import org.dalmatian.common.redis.annotation.Cacheable;
import org.dalmatian.common.redis.cache.CacheManager;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.Solon;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.util.SupplierEx;
import org.noear.solon.data.util.StringMutexLock;
import org.noear.solon.expression.snel.SnEL;

/**
 * @author zc
 */
@Slf4j
public class CacheExecutor {

    public static final CacheExecutor global = new CacheExecutor();
    private final StringMutexLock SYNC_LOCK = new StringMutexLock();

    public CacheExecutor() {
    }

    public Object cacheAble(Cacheable anno, Invocation inv, SupplierEx executor) throws Throwable {
        // 取出 key 值
        String key = getKey(anno.key(), inv);
        String value = anno.value();
        String keyStr = key + value;
        if (key == null || value == null) {
            log.warn("缓存失效，请补充 key 或 value");
            return executor.get();
        }
        Object result;
        SYNC_LOCK.lock(keyStr);
        try {
            CacheManager cacheManager = Solon.context().getBean(CacheManager.class);
            result = cacheManager.getCache(value).get(key);
            // 直接返回缓存内的数据
            if (result != null) {
                return result;
            } else {
                result = executor.get();
                cacheManager.getCache(value).put(key, result);
                return result;
            }
        } finally {
            SYNC_LOCK.unlock(keyStr);
        }
    }

    /**
     * 清除 key
     *
     * @param anno
     * @param inv
     */
    public void cacheEvict(CacheEvict anno, Invocation inv,Object result) {
        // 取出 key 值
        String key = getKey(anno.key(), inv);
        String value = anno.value();
        String keyStr = key + value;
        if (key == null || value == null) {
            log.warn("缓存删除失效，请补充 key 或 value");
            return;
        }
        CacheManager cacheManager = Solon.context().getBean(CacheManager.class);
        cacheManager.getCache(value).evict(key);

    }

    // 更新數據
    public void cachePut(CachePut anno, Invocation inv, Object result) {
        // 取出 key 值
        String key = getKey(anno.key(), inv);
        String value = anno.value();
        String keyStr = key + value;
        if (key == null || value == null) {
            log.warn("缓存失效，请补充 key 或 value");

        }
        CacheManager cacheManager = Solon.context().getBean(CacheManager.class);
        cacheManager.getCache(value).putIfAbsent(key, result);
    }

     private String getKey(String key,Invocation inv){
       key = key.replaceAll("#","");
       Map<String,Object>  context = inv.argsAsMap();
       return   String.valueOf(SnEL.eval(key,context));
    }
}

